package com.deep.seckill.controller.web;

import com.deep.common.utils.R;
import com.deep.seckill.model.dto.SeckillSkuRedisDTO;
import com.deep.seckill.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 秒杀控制器
 *
 * @author Deep
 * @date 2022/2/24
 */
@Controller
@RequestMapping("api/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    /**
     * 获取当前时间参与秒杀的所有商品
     */
    @ResponseBody
    @GetMapping("/getCurrentSeckillSkus")
    public List<SeckillSkuRedisDTO> getCurrentSeckillSkus() {
        return seckillService.getCurrentSeckillSkus();
    }

    /**
     * 根据skuId查询商品是否参加秒杀活动
     */
    @ResponseBody
    @GetMapping(value = "/sku/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
        SeckillSkuRedisDTO seckillSkuRedisDTO = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().put("data", seckillSkuRedisDTO);
    }

    /**
     * 商品进行秒杀(秒杀开始)
     * 查看表 oms_order_item
     */
    @GetMapping(value = "/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        try {
            String orderSn = seckillService.seckill(killId, key, num);
            model.addAttribute("orderSn", orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}
